package com.ddyx.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ddyx.common.util.LoginResponse;
import com.ddyx.common.util.ResponseJson;
import com.ddyx.entity.Store;
import com.ddyx.mapper.StoreMapper;
import com.ddyx.service.IStoreService;

@Service("storeService")
public class StoreServiceImpl implements IStoreService {

	@Resource
	StoreMapper storeMapper;
	@Override
	public String addStore(JSONObject object) {
		// TODO Auto-generated method stub
		String storeName = object.getString("storeName");
		String address = object.getString("address");
		String location = object.getString("location");
		String phone = object.getString("phone");
		Store store = new Store();
		try {
			store.setCompanyId(1);
		//	store.setStoreNo(storeNo);
			store.setStoreName(storeName);
			store.setAddress(address);
			store.setLocation(location);
			store.setPhone(phone);
			boolean b = storeMapper.insert(store);
			if (!b) {
				return LoginResponse.getInstance().setStatus(false)
						.setMessage("新增门店失败")
						.setData(new JSONObject()).toString();
			}
		}catch (Exception e) {
			return LoginResponse.getInstance().setStatus(false)
					.setMessage(e.toString())
					.setData(new JSONObject()).toString();
		}
		return LoginResponse.getInstance().setStatus(true).setMessage("").setData(store).toString();
	}

	@Override
	public String editStore(JSONObject object) {
		// TODO Auto-generated method stub
		int storeId = object.getIntValue("storeId");
		String storeName = object.getString("storeName");
		String address = object.getString("address");
		String location = object.getString("location");
		String phone = object.getString("phone");
		Store store = new Store();
		try {
			store.setStoreId(storeId);
			store.setStoreName(storeName);
			store.setAddress(address);
			store.setLocation(location);
			store.setPhone(phone);
			boolean b = storeMapper.updateByPrimaryKey(store);
			if (!b) {
				return LoginResponse.getInstance().setStatus(false)
						.setMessage("修改门店信息失败")
						.setData(new JSONArray()).toString();
			}
		} catch (Exception e) {
			return LoginResponse.getInstance().setStatus(false)
					.setMessage(e.toString())
					.setData(new JSONArray()).toString();
		}
		return  LoginResponse.getInstance().setStatus(true).setMessage("").setData(new JSONArray()).toString();
	}

	@Override
	public String selectStoreByName(Map<String, Object> params) {
		// TODO Auto-generated method stub
		int size = params.get("size") == null ? 0 : Integer.valueOf(params.get("size") + "");
		int page = params.get("page") == null ? 0 : Integer.valueOf(params.get("page") + "");
		int start = 0;
		if (size < 1)
			size = 20;// 默认每页20条
		if (page < 1)
			page = 1;// 默认第1页
		start = (page - 1) * size;
		params.put("start", start);
		params.put("size", size);
		List<Store> list = storeMapper.selectByStoreName(params);
		int count = storeMapper.selectCountByStoreName(params);
		return ResponseJson.getInstance().setPage(page).setSize(size).setTotal(count).setData(list).toString();
	}

}
